<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class StaffDormAdjust.
 *
 * @package namespace App\Models;
 */
class StaffDormAdjust extends BaseModel implements Transformable
{
    use TransformableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['staff_id', 'dorm_id_from', 'dorm_id_to', 'adjust_date', 'adjust_reason'];
    protected $appends = ['adjust_type'];

    public function staff()
    {
        return $this->belongsTo(Staff::class);
    }

    public function dormFrom()
    {
        return $this->belongsTo(Dorm::class, 'dorm_id_from');
    }

    public function dormTo()
    {
        return $this->belongsTo(Dorm::class, 'dorm_id_to');
    }

    public function getAdjustTypeAttribute()
    {
        if (array_has($this->attributes, 'dorm_id_from') && array_has($this->attributes, 'dorm_id_to')) {
            if ($this->attributes['dorm_id_from'] && $this->attributes['dorm_id_to']) {
                return 'adjust';
            } elseif ($this->attributes['dorm_id_from']) {
                return 'out';
            } else {
                return 'in';
            }
        } else {
            return '';
        }
    }
}
